package com.xc.controller;

import com.xc.dto.ResultDTO;
import com.xc.entity.StudentEntity;
import com.xc.entity.UserEntity;
import com.xc.service.StudentService;
import com.xc.service.UserService;
import com.xc.util.Tool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

//restful 增加post，修改put，删除delete，查询get
@RestController
@RequestMapping("/xc/student")
@Api(tags = "学生管理")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private StudentEntity studentEntity;

    @ApiOperation("学生增加")
    @PostMapping()
    public ResultDTO add(StudentEntity studentEntity) {

        studentEntity.setPage(Tool.strToPage(studentEntity.getPage() + ""));
        studentEntity.setLimit(Tool.strToLimit(studentEntity.getLimit() + ""));
        return studentService.add(studentEntity);
    }

    @ApiOperation("学生修改")
    @PutMapping()
    public ResultDTO update(StudentEntity studentEntity) {
        return studentService.update(studentEntity);
    }

    @ApiOperation("学生修改状态enable，1表示正常，0表示未达到标准")
    @PutMapping("/enable")
    public ResultDTO updateEnable(StudentEntity studentEntity) {
        return studentService.updateEnable(studentEntity);
    }

    @ApiOperation("根据id删除学生信息")
    @DeleteMapping("/{id}")
    public ResultDTO delete(StudentEntity studentEntity) {
        return studentService.delete(studentEntity);
    }

    @ApiOperation("查询所有")
    @GetMapping()
    public ResultDTO findAll(StudentEntity studentEntity) {
        studentEntity.setPage(Tool.strToPage(studentEntity.getPage() + ""));
        studentEntity.setLimit(Tool.strToLimit(studentEntity.getLimit() + ""));
        return studentService.findAll(studentEntity);
    }

    @ApiOperation("查询所有,且只查询所有学员的最新数据")
    @PostMapping("/a")
    public ResultDTO a(@ApiParam(value = "账号", required = true) @RequestParam String name,
                       @ApiParam(value = "性别") @RequestParam String sex,
                       @ApiParam(value = "年龄") @RequestParam Integer age) {
        studentEntity.setName(name);
        studentEntity.setSex(sex);
        studentEntity.setAge(age);
        studentEntity.setPage(Tool.strToPage(studentEntity.getPage() + ""));
        studentEntity.setLimit(Tool.strToLimit(studentEntity.getLimit() + ""));
        System.out.println(studentEntity);
        return studentService.findAll(studentEntity);
    }

    @ApiOperation("根据id查询学生信息")
    @GetMapping("/{id}")
    public ResultDTO findById(StudentEntity studentEntity) {
        studentEntity.setPage(Tool.strToPage(studentEntity.getPage() + ""));
        studentEntity.setLimit(Tool.strToLimit(studentEntity.getLimit() + ""));
        return studentService.findById(studentEntity);
    }
    @ApiOperation("根据uuid查询学生信息")
    @GetMapping("/uuid/{uuid}")
    public ResultDTO findByUuid(StudentEntity studentEntity) {
        studentEntity.setPage(Tool.strToPage(studentEntity.getPage() + ""));
        studentEntity.setLimit(Tool.strToLimit(studentEntity.getLimit() + ""));
        return studentService.findByUuid(studentEntity);
    }
    @GetMapping("/count")
    public ResultDTO count(StudentEntity studentEntity) {
        return studentService.count(studentEntity);
    }
}
